package com.rambo.sdh.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.rambo.sdh.dao.inter.DepartmentDao;
import com.rambo.sdh.dao.pojo.Department;
import com.rambo.sdh.dao.pojo.Employee;

@Component
public class DepartmentDaoImpl implements DepartmentDao {

	@Autowired
	private SessionFactory sessionFactory;

	public Session getSession() {
		return sessionFactory.getCurrentSession();

	}

	@Override
	public Department getDepartmentByid(Integer id) {

		Session session = this.getSession();

		Department department =  (Department) session.get(Department.class, id);
		return department;

	}

	@Override
	public Department getDePartmentByemployeeId(Integer id) {

		Session session = this.getSession();

		String sql = "select d.* from department d ,employee e where  d.id=e.department_id and e.id=?";
		Department department = (Department) session.createSQLQuery(sql).addEntity(Department.class).setInteger(0, id).uniqueResult();

		return department;
	}

	@Override
	public List<Employee> getemployeeByDepartmentId(Integer id) {

		Session session = this.getSession();

		String sql = "select e.* from department d,employee e where  d.id=e.department_id and d.id=?";
		List<Employee> employees = (List<Employee>) session.createSQLQuery(sql)
				.setInteger(0, id).list();
		return employees;
	}

}
